<?php
ob_start();
require_once("connect.php");
require_once("misc.php");
if(!logged()){
	header("Location:index.php"); die;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<title>User Dashboard</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
	<link rel="stylesheet" href="src/style.css">
	<link rel="icon" type="image/png" href="icon.png">
	
	<script src="bootstrap/js/bootstrap.min.js"></script>
	<script src="src/jquery-2.1.4.js"></script>
	<script type="text/javascript">
	function logout(){
		$(location).attr('href', 'userLogout.php')
	}
	function refresh(){
		$("#yourUrl").load(location.href + " #yourUrl");
	}
	function deleteUrl(id){
		var tr = $("tr[id=data"+id+"]");
		var btnStr = "button[name=del"+id+"]";
		var btn = $(btnStr);
		tr.css("background-color","#ffaaaa");
		var conf = confirm("Really delete this data?");
		if(conf){
			enableBtn(btnStr,false);
			btn.text("Deleting..");
			$.post("checkDel.php",{id:id}, function(result){
				result = trim(result);
				if(result == "success"){
					//delete the table row
					tr.remove();
				}
			});
		}
		tr.css("background-color","");
	}
	</script>
</head>
<body>
<div class="form-group">
<h4>Kristo URL Shortener - User Dashboard</h4>
Welcome, <?php echo user(0); ?> &nbsp
<button name="logoutBtn" onclick="logout()">Logout</button>
<?php include("shorten.php"); ?>

<h4>Your URLs</h4>
<div class="form-group" id="yourUrl">
<table>
<?php
$que = mysqli_query($db,"select * from urltable where owner=".user(1));
while($row = mysqli_fetch_assoc($que)){
	$id = $row["id"];
	$short = getShortUrl($row["link"],$id);
	echo "<tr id='data$id'><td>"."<a href='http://$short' target='_blank'>$short</a>"."</td>";
	$fullurl = $row["fullurl"];
	echo "<td><a href='$fullurl' target='_blank'>$fullurl</a></td>";
	echo "<td><button name='del$id' onclick='deleteUrl($id)'>Delete</button></td></tr>";
}
?>
</table>
</div>
</body>
</html>
<?php ob_end_flush(); ?>